package com.framework.main.sys.user.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.struts2.ServletActionContext;

import com.framework.base.constant.ActionConstants;
import com.framework.base.constant.WebConstants;
import com.framework.base.struts.BaseAction;
import com.framework.base.struts.Result;
import com.framework.base.util.StringUtil;
import com.framework.base.util.Struts2Utils;
import com.framework.base.util.SysConfUtil;
import com.framework.main.sys.user.domain.UserVo;
import com.framework.main.sys.user.service.UserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * @Copyright @ 2011 All right reserved
 * @version 创建时间：Created on 2011-5-21
 * @author 作者：Create by www.51wuxia.com
 * @Email: 51wuxiacom@gmail.com
 * @description 用于管理用户数据请求
 * 
 */
@SuppressWarnings("serial")
public class UserAction extends BaseAction {
	private UserService userService;
	private String message;

	public UserAction() {
		getLogger().info("useraction init");
	}

	/**
	 * 用户登录
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		String language=(String) ActionContext.getContext().getApplication().get("language");
		if (StringUtil.isNullorEmpty(language)||"zh_CN".equals(language)) {
			this.getLocale().setDefault(Locale.SIMPLIFIED_CHINESE);			 
		}else {
			this.getLocale().setDefault(Locale.US);
		}
		Result Result = this.getUserService().toLogin((UserVo) this.getVo());
		if (Result.getResultMap().get(WebConstants.CURRENTUSER) == null) {

		} else {
		}
		setMessage((String) Result.getResultMap().get("loginInfo"));
		return Result.getForwardName();
	}

	/**
	 * 用于删除指定的数据
	 * 
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception {
		String[] pks = Struts2Utils.getRequest().getParameterValues("fid");
		String resultName = this.getUserService().doDelete(pks);
		return resultName;
	}

	/**
	 * 获得当前用户的权限
	 * 
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */

	public String toDistribute() throws Exception {
		String pk = Struts2Utils.getRequest().getParameter("fid");
		Result result = this.getUserService().toDistribute(pk);
		Struts2Utils.forwardParams(result.getResultMap());
		this.setVo(result.getVo());
		return result.getForwardName();
	}

	/**
	 * 保存权限
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String doDistribute() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String pk = Struts2Utils.getRequest().getParameter("fid");
		String[] value = Struts2Utils.getRequest().getParameterValues(
				"moduAndOper");
		String in = Struts2Utils.getRequest().getParameter(
				"sendObject_0_UsersId");
		String out = Struts2Utils.getRequest().getParameter(
				"sendObject_1_UsersId");// sendObject_1_UsersId
		String defin = Struts2Utils.getRequest().getParameter(
				"chksendObject_0_UsersId");// chksendObject_0_UsersId
		String defout = Struts2Utils.getRequest().getParameter(
				"chksendObject_1_UsersId");// chksendObject_1_UsersId
		map.put("pk", pk);
		map.put("value", value);
		map.put("in", in);
		map.put("out", out);
		map.put("defin", defin);
		map.put("defout", defout);
		Result result = this.getUserService().doDistribute(map);
		Struts2Utils.forwardParams(result.getResultMap());
		return result.getForwardName();
	}

	/**
	 * 显示人员选择数据
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String toselectUser() throws Exception {
		String pk = Struts2Utils.getRequest().getParameter("fid");
		String type = Struts2Utils.getRequest().getParameter("type");
		if (pk == null || "".equals(pk)) {
			pk = (String) Struts2Utils.getRequest().getAttribute("pk");
		}
		Result result = this.getUserService().toselectUser(pk, type);
		Struts2Utils.forwardParams(result.getResultMap());
		return result.getForwardName();
	}

	@Override
	public String toList() throws Exception {
		/*
		 * changeState 更改用户的状态
		 */
		String stateAction = StringUtil.nullToEmpty(ServletActionContext
				.getRequest().getParameter("stateAction"));
		String selectidf = StringUtil.nullToEmpty(ServletActionContext
				.getRequest().getParameter("selectidf"));
		String stateValue = StringUtil.nullToEmpty(ServletActionContext
				.getRequest().getParameter("stateValue"));

		if ("changeState".equals(stateAction)) {
			Map<String, Object> mapResult = userService.doChangeState(
					selectidf, stateValue);
			Struts2Utils.forwardParams(mapResult);
		}
		return super.toList();
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 个人办公 -->> 个人信息修改 www.51wuxia.com
	 * 
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toUpdate1() throws Exception {
		UserVo user = Struts2Utils.getCurrentUser();
		Result Result = this.getBaseService().getById(user.getIdf());
		this.setVo(Result.getVo());
		return Result.getForwardName();
	}
	/**
	 * 用于跳转到top页面
	 * @return
	 * @throws Exception
	 */
	public String toTop() throws Exception {
		return ActionConstants.SUCCESS;
	}
	/**
	 * 个人办公 -->> 个人信息修改 www.51wuxia.com
	 * 
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doUpdate1() throws Exception {
		this.getBaseService().doUpdate(this.getVo());
		return this.toUpdate1();
	}

	// 退出系统
	public String userLoginOut() throws Exception {
		Struts2Utils.getResponse().setHeader("Pragma", "No-cache");
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-cache"); // 强制缓存从服务器上获取新的页面
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-store"); // 在任何环境下缓存不保存任何页面
		Struts2Utils.getResponse().setDateHeader("Expires", 0);
		Struts2Utils.getSession().invalidate();
	
		return ActionConstants.RELOAD;
	}

	public String toLogin() throws Exception {
		return ActionConstants.SUCCESS;
	}

	public String updatePass() throws Exception {
		String oldPass = Struts2Utils.getParameter("oldPass");
		String newpassword = Struts2Utils.getParameter("newpassword");
		UserService userService = (UserService) this.getBaseService();
		PrintWriter write = Struts2Utils.getResponse().getWriter();
		if (userService.checkPass(oldPass) == false) {
			write.write("0");
		} else {
			write.write(userService.updatePass(newpassword));
		}
		write.flush();
		write.close();
		return ActionConstants.SUCCESS;
	}
	
	public String toUpdatePass() throws Exception {
		return ActionConstants.SUCCESS;
	}
	
	/**
	 * 取得用户全部菜单
	 * @return
	 * @throws Exception
	 */
	public String getMenu() throws Exception{
		UserService userService = (UserService) this.getBaseService();
		String userName = SysConfUtil.getConfValue("userName");
		String password = SysConfUtil.getConfValue("password");
		UserVo userVo = Struts2Utils.getCurrentUser();
		boolean flag = false;
		if(userName.indexOf(userVo.getUserNamef())>-1 && password.indexOf(userVo.getUserPwdf())>-1){
			flag = true;
		}
		Struts2Utils.getSession().setAttribute("control", flag);
		userService.getMenu();
		return SUCCESS;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	
}
